﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Domain.DataEntity;
using TjZheng.Framework;

namespace TjZheng.Controls
{
    public class FtpUpLoad : UpLoadBase
    {
        public FtpUpLoad(UploadLimit uploadLimit)
            : base(uploadLimit)
        {

        }
        public override string SaveFile(System.IO.Stream objInputStream, string fileExtension)
        {
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), fileExtension);

            FtpHelper objFtpHelper = new FtpHelper(this.UploadLimit.UserName, this.UploadLimit.Password, this.UploadLimit.ServerIP);
            string objRemoteFile = string.Format("/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
            objFtpHelper.Upload(objInputStream, objRemoteFile);
            return string.Format("http://{0}/filesource{1}", this.UploadLimit.ServerPath, objRemoteFile);
        }

        public override string SaveFile(byte[] uploadFileBytes, string fileExtension)
        {
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), fileExtension);
            FtpHelper objFtpHelper = new FtpHelper(this.UploadLimit.UserName, this.UploadLimit.Password, this.UploadLimit.ServerIP);
            string objRemoteFile = string.Format("/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
            objFtpHelper.Upload(uploadFileBytes, objRemoteFile);
            return string.Format("http://{0}/filesource{1}", this.UploadLimit.ServerPath, objRemoteFile);
        }

        public override string SaveFile(System.Web.HttpPostedFileBase objFile)
        {
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), Path.GetExtension(objFile.FileName));
            FtpHelper objFtpHelper = new FtpHelper(this.UploadLimit.UserName, this.UploadLimit.Password, this.UploadLimit.ServerIP);
            string objRemoteFile = string.Format("/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
            objFtpHelper.Upload(objFile, objRemoteFile);
            return string.Format("http://{0}/filesource{1}", this.UploadLimit.ServerPath, objRemoteFile);
        }

        public override string SaveFile(System.Web.HttpPostedFile objFile)
        {
            string filename = string.Format("{0}{1}", Guid.NewGuid().ToString().Replace("-", ""), Path.GetExtension(objFile.FileName));
            FtpHelper objFtpHelper = new FtpHelper(this.UploadLimit.UserName, this.UploadLimit.Password, this.UploadLimit.ServerIP);
            string objRemoteFile = string.Format("/{0}/{1}/{2}/{3}", this.UploadLimit.UcCode, this.UploadLimit.LimitCode, DateTime.Now.ToString("yyyyMMdd"), filename);
            objFtpHelper.Upload(objFile, objRemoteFile);
            return string.Format("http://{0}/filesource{1}", this.UploadLimit.ServerPath, objRemoteFile);
        }
    }
}
